﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtletIS.Shared.Entities;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Forms
{
    public partial class frmSeqStartTime : Form
    {
        private DisciplineListEntity discipline;

        public frmSeqStartTime(DisciplineListEntity discipline)
        {
            InitializeComponent();

            this.discipline = discipline;
            this.Icon = Properties.Resources.AtletIS64;
        }

        private void frmSeqStartTime_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = this.discipline.Name;
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                // Get performances
                Data.aDbDataSet.PerformancesDataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(this.discipline.ID);
                DateTime seqStartTime = this.dtpSeqStartTime.Value;

                

                if (dtPerformances != null && dtPerformances.Rows.Count > 0)
                {
                    DataRow[] drPerformances;

                    if (this.chkStartNumber.Checked)
                        drPerformances = dtPerformances.Select(null, "StartNumber");
                    else
                        drPerformances = dtPerformances.Select(null, "Lane");
                    
                    PerformancesTableAdapter taPerformances = new PerformancesTableAdapter();

                    foreach (DataRow dr in drPerformances)
                    {
                        dr["RealStartTime"] = seqStartTime;
                        seqStartTime = seqStartTime.AddSeconds(Convert.ToDouble(nudDelay.Value));
                    }

                    // Save performances
                    taPerformances.Update(dtPerformances);
                    MessageBox.Show("Generovanie prebehlo úspešne.", "Sekvenčné štarty", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            try
            {
                // Get performances
                Data.aDbDataSet.PerformancesDataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(this.discipline.ID);

                if (dtPerformances != null && dtPerformances.Rows.Count > 0)
                {
                    PerformancesTableAdapter taPerformances = new PerformancesTableAdapter();

                    foreach (DataRow dr in dtPerformances.Rows)
                    {
                        dr["RealStartTime"] = DBNull.Value;
                    }

                    // Save performances
                    taPerformances.Update(dtPerformances);
                    MessageBox.Show("Zrušenie prebehlo úspešne.", "Sekvenčné štarty", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }
    }
}
